//
//  YDCustomerDetailController.m
//  YDTestDrive
//
//  Created by ios on 2017/2/14.
//  Copyright © 2017年 ios. All rights reserved.
//

#import "YDCustomerDetailController.h"

#import "YDUserInfoCell.h"
#import "YDCustomerDetailFooterView.h"

#import "YDCustomerListModel.h"

#import "UIImageView+WebCache.h"

@interface YDCustomerDetailController () <UITableViewDataSource,UITableViewDelegate>

//点击图片放大使用的
{
    UIView *blackBGView;
    UIView *coverView;
    UIImageView *bigImageView;
    CGRect imageInWindowFrame;
    CGSize imageBigSize;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) YDCustomerDetailFooterView *footerView;

@end

@implementation YDCustomerDetailController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"客户详情页";
    
    [self createView];
}

- (void)createView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64.0f - 49.0f) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib(@"YDUserInfoCell")];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = kViewControllerBackgroundColor;
    [self.view addSubview:_tableView];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 15.0f)];
    headerView.backgroundColor = kViewControllerBackgroundColor;
    _tableView.tableHeaderView = headerView;
    
    _footerView = [YDCustomerDetailFooterView instanceCustomerDetailFooterView];
    NSArray *urlArray = [self.customerListModel.licenceImgUrl componentsSeparatedByString:@","];
    if (urlArray.count >= 1) {
        [_footerView.cardImage1 sd_setImageWithURL:[NSURL URLWithString:[urlArray objectAtIndex:0]] placeholderImage:[UIImage imageNamed:@""]];
    }
    
    if (urlArray.count >= 2) {
        [_footerView.cardImage2 sd_setImageWithURL:[NSURL URLWithString:[urlArray objectAtIndex:1]] placeholderImage:[UIImage imageNamed:@""]];
    }
    
    _tableView.tableFooterView = _footerView;
    
    
    //初始化一个用来当做背景的View
    blackBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    blackBGView.backgroundColor = [UIColor clearColor];
    blackBGView.hidden = YES;
    [kWindow addSubview:blackBGView];
    
    //黑色背景，渐变
    coverView = [[UIView alloc] initWithFrame:blackBGView.bounds];
    coverView.backgroundColor = [UIColor blackColor];
    [blackBGView addSubview:coverView];
    
    bigImageView = [[UIImageView alloc] init];
    [blackBGView addSubview:bigImageView];
    
    //添加点击手势（即点击图片后退出全屏）
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeView)];
    [blackBGView addGestureRecognizer:tapGesture];
    
    //添加点击手势(三个ImageView的交互属性在xib文件中打开的)
    UITapGestureRecognizer *imageTap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    UITapGestureRecognizer *imageTap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [_footerView.cardImage1 addGestureRecognizer:imageTap1];
    [_footerView.cardImage2 addGestureRecognizer:imageTap2];
}

#pragma mark - ==================tableview delegate==================
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YDUserInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YDUserInfoCell"];
    cell.lineView.hidden = NO;
    cell.titleLabel.textColor = [UIColor colorWithHexString:@"7a8694"];
    cell.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    cell.subTitleLabel.textColor = [UIColor colorWithHexString:@"354052"];
    cell.subTitleLabel.font = [UIFont systemFontOfSize:15.0f];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.row == 0) {
        cell.titleLabel.text = @"姓名:";
        cell.subTitleLabel.text = self.customerListModel.customerName;
    } else if (indexPath.row == 1) {
        cell.titleLabel.text = @"手机:";
        cell.subTitleLabel.text = self.customerListModel.customerPhone;
    } else if (indexPath.row == 2) {
        cell.titleLabel.text = @"试驾时间:";
        NSDate *date = [NSDate dateWithString:self.customerListModel.doneTimeString format:@"yyyy-MM-dd HH:mm:ss"];
        cell.subTitleLabel.text = [NSDate stringWithDate:date format:@"yyyy年MM月dd日 HH:mm"];
    } else {
        cell.titleLabel.text = @"试驾车型:";
        cell.subTitleLabel.text = self.customerListModel.cartypeName;
        cell.lineView.hidden = YES;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55.0f;
}


#pragma mark - 点击图片放大图片
//点击图片后的方法(即图片的放大全屏效果) (图片的tag分别为101、102、103)
- (void)tapAction:(UITapGestureRecognizer *)tapGesture
{
    CGFloat imageX = _footerView.cardImage1.x;
    CGFloat imageY = _footerView.cardImage1.y + 64.0f + 245.0f;
    
    if (tapGesture.view.tag == 101)
    {
        if (_footerView.cardImage1.image == nil) return;
        
        bigImageView.image = _footerView.cardImage1.image;
        imageX = _footerView.cardImage1.x;
    }
    else if (tapGesture.view.tag == 102)
    {
        if (_footerView.cardImage2.image == nil) return;
        
        bigImageView.image = _footerView.cardImage2.image;
        imageX = _footerView.cardImage2.x;
    }
    
    //计算image放大后的size
    CGFloat imageHeight = bigImageView.image.size.height / bigImageView.image.size.width * kScreenWidth;
    imageBigSize = CGSizeMake(kScreenWidth, imageHeight);
    
    imageInWindowFrame = CGRectMake(imageX, imageY, _footerView.cardImage1.width, _footerView.cardImage1.height);
    
    //计算点击的图片在整个屏幕的位置 和 图片放大后的大小 (放大和缩小用)
    bigImageView.frame = imageInWindowFrame;
    
    coverView.alpha = 0;
    blackBGView.hidden = NO;
    [UIView animateWithDuration:0.25f animations:^{
        coverView.alpha = 1.0f;
        bigImageView.size = imageBigSize;
        bigImageView.center = blackBGView.center;
    } completion:^(BOOL finished) {
        blackBGView.userInteractionEnabled = YES;
    }];
}
//缩小图片
- (void)closeView
{
    [UIView animateWithDuration:0.25f animations:^{
        bigImageView.frame = imageInWindowFrame;
        coverView.alpha = 0;
    } completion:^(BOOL finished) {
        blackBGView.hidden = YES;
    }];
}

@end
